<template>
  <div v-show="dialogValue" class="add-form f-c">
    <div class="header">
      <div class="title">
        添加/编辑
      </div>
      <el-button @click="onPageBack">返回</el-button>
    </div>
    <div class="add-form-content f-g">
      <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @close="onPageBack"
        @save="onSave"
      />
    </div>
  </div>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  data() {
    return {
      dialogValue: false,
      ruleForm: {
      },
      rules: {
        expertName: [{ required: true, message: '不能为空' }],
        expertType: [{ required: true, message: '不能为空' }],
        phone: [{ required: true, message: '不能为空' }],
      },
      formList: [
        {
          key: 'expertName',
          name: 'input',
          label: '专家名称',
          colSpan: 12
        },
        {
          key: 'phone',
          name: 'input',
          label: '手机号码',
            colSpan: 12
        },
        {
          key: 'expertType',
          name: 'select',
          label: '专家类型',
          options: [
              { value: '1', name: '社会专家' },
              { value: '2', name: '职能科室专家' },
              { value: '3', name: '建设单位' }
          ],
          colSpan: 12
        },
        {
          key: 'industryName',
            name: 'select',
            label: '行业',
            options: [],
            colSpan: 12
        },
        {
          key: 'industryExpertise',
          name: 'input',
          type: 'textarea',
          rows: 10,
          label: '专业特长'
        }
      ]
    }
  },
    created() {
      http.professor.hangye().then(res => {
          this.formList[3].options = res.data.map(el => {
              return {
                  value: el,
                  name: el
              }
          })
      })
    },
    methods: {
    getForm() {
      const form = {
        id: undefined
      }
      this.formList.forEach(el => {
        form[el.key] = ''
      })
      form.expertType = form.expertType.toString()
      return form
    },
    async open(item) {
      this.ruleForm = this.getForm()
      if (item) {
        for (const key in this.ruleForm) {
          this.ruleForm[key] = item[key]
        }
      }
      console.log(this.ruleForm)
      this.ruleForm.expertType = this.ruleForm.expertType.toString()
      this.dialogValue = true
    },
    onPageBack() {
      this.$emit('get-page')
      this.dialogValue = false
    },
    onSave() {
      http.professor[this.ruleForm.id ? 'update' : 'create']({ ...this.ruleForm }).then(res => {
        this.$message.success(res.msg)
        this.onPageBack()
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
